use clap::{Parser, Subcommand};//用于解析命令行参数
use tracing::{info, error};//用于结构化日志记录

mod server;
mod agent;
mod common;
mod cli;

#[derive(Parser)]
#[command(name = "oc_manager")]
#[command(about = "OpenCloudOS大规模集群节点管理工具")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// 启动Server端
    Server {
        /// 监听端口
        #[arg(short, long, default_value = "8443")]
        port: u16,
        /// 配置文件路径
        #[arg(short, long, default_value = "config/server.toml")]
        config: String,
    },
    /// 启动Agent端
    Agent {
        /// Server地址
        #[arg(short, long, default_value = "localhost:8443")]
        server: String,
        /// 配置文件路径
        #[arg(short, long, default_value = "config/agent.toml")]
        config: String,
    },
    /// 生成TLS证书
    Cert {
        /// 证书输出目录
        #[arg(short, long, default_value = "certs")]
        output: String,
    },
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // 初始化日志
    tracing_subscriber::fmt()
        .with_env_filter("oc_manager=info")
        .init();

    info!("启动OpenCloudOS集群管理工具");

    let cli = Cli::parse();

    match cli.command {
        Commands::Server { port, config } => {
            info!("启动Server端，端口: {}, 配置: {}", port, config);
            server::run(port, config).await?;
        }
        Commands::Agent { server, config } => {
            info!("启动Agent端，连接Server: {}, 配置: {}", server, config);
            agent::run(server, config).await?;
        }
        Commands::Cert { output } => {
            info!("生成TLS证书到目录: {}", output);
            cli::cert::generate_certs(&output)?;
        }
    }

    Ok(())
} 